import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Properties;
import java.util.Set;

import javax.swing.text.MutableAttributeSet;
import javax.swing.text.html.HTML;
import javax.swing.text.html.HTML.Tag;
import javax.swing.text.html.HTMLEditorKit;

public class FetchURLS {
	public List<String> getMediaUrls(String url) throws Throwable{
		URL url2 = new URL(url);
		StringReader reader = new StringReader(getContentOfUrl(url2));
		HTMLEditorKit.Parser parser = new HTMLParser().getParser();
		HTMLParserCallBackHander hander = new HTMLParserCallBackHander(url2);
		parser.parse(reader, hander, true);
		return hander.getParsedUrls();
	}
	private String getContentOfUrl(URL url) throws Throwable{
		StringBuilder content = new StringBuilder();
		URLConnection connection = url.openConnection();
		connection.setConnectTimeout(100000);
		connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
		BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(),"UTF-8"));
		String tmp = null;
		while((tmp =reader.readLine()) != null){
			content.append(tmp);
		}
		reader.close();
		return content.toString();
	}
}
class HTMLParser extends HTMLEditorKit {
	private static final long serialVersionUID = 1L;
	@Override
	public HTMLEditorKit.Parser getParser(){
		return super.getParser();
	}
}
class HTMLParserCallBackHander extends HTMLEditorKit.ParserCallback{
	private final static Set<String> mediaExt  = new HashSet<String>();
	static{
		mediaExt.add("mp3".toLowerCase());
		mediaExt.add("wma".toLowerCase());
		mediaExt.add("wmv".toLowerCase());
		mediaExt.add("avi".toLowerCase());
		mediaExt.add("mkv".toLowerCase());
		mediaExt.add("mp4".toLowerCase());
		Properties properties = new Properties();
		try{
			properties.load(FetchURLS.class.getResourceAsStream("/application.properties"));
		}catch(Throwable t){
			System.err.println(String.format("Unble to load properties file. %S", t.getLocalizedMessage()));
		}
		String mediaFileExtensions = properties.getProperty("mediaFileExtensions");
		for(String mediaFileExtn : mediaFileExtensions.split(",")){
			mediaExt.add(mediaFileExtn.toLowerCase());
		}
	}
	private String baseUrl;
	private URL url;
	private List<String> urls = new ArrayList<String>();
	public HTMLParserCallBackHander(URL url){
		this.baseUrl = url.getProtocol() + "://" + url.getHost() + ":" + url.getPort();
		this.url = url;
	}
	@Override
	public void handleStartTag(Tag t, MutableAttributeSet a,int pos) {
		if(HTML.Tag.A == t){
			String value  = (String) a.getAttribute(HTML.Attribute.HREF);
			if(value != null && !"".equals(value)){
				int lastIndexOf = value.lastIndexOf(".");
				if(lastIndexOf != -1){
					String urlMediaExt = value.substring(lastIndexOf + 1).toLowerCase();
					int queryPos = urlMediaExt.indexOf('?');
					if (queryPos != -1){
						urlMediaExt = urlMediaExt.substring(0, queryPos);
					}
					if(mediaExt.contains(urlMediaExt)){
						//urls.add(baseUrl + value);
						try{
							URI uri = new URI(url.getProtocol(), url.getHost() + ":" + url.getPort(), value);
							System.out.println(baseUrl +   uri.toURL().toString().substring(uri.toURL().toString().indexOf('#') + 1));//HARD CODED VALUE
							urls.add(baseUrl +   uri.toURL().toString().substring(uri.toURL().toString().indexOf('#') + 1));
						}catch(Throwable throwable){}
					}
				}
			}	
		}
	}
	public List<String> getParsedUrls(){
		return urls;
	}
	
}